//
//  ScrollViewChangeViewController.m
//  iOS UI Sample Code
//
//  Created by 규성 이 on 12. 2. 23..
//  Copyright (c) 2012년 kkamci77@gmail.com. All rights reserved.
//

#import "ScrollViewChangeViewController.h"

@implementation ScrollViewChangeViewController

@synthesize svScrollView, vView1, vView2, vView3, showSubView, topViewPoint;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.showSubView = NO;
    // Do any additional setup after loading the view from its nib.
    [self.vView1 setBackgroundColor:UIColor.blackColor];
    [self.vView2 setBackgroundColor:UIColor.blueColor];
    [self.vView3 setBackgroundColor:UIColor.redColor];
    [self.vView2 setFrame:CGRectMake(0.0f, -[self.vView2 bounds].size.height + 44, [self.vView2 bounds].size.width, [self.vView2 bounds].size.height)];
    [self.vView3 setFrame:CGRectMake(0.0f, -[self.vView2 bounds].size.height, [self.vView2 bounds].size.width, [self.vView2 bounds].size.height)];
    self.topViewPoint = CGPointMake(0.0f, -[self.vView2 bounds].size.height + 44);
    [self.svScrollView setDelegate:self];
    [self.svScrollView setContentSize:CGSizeMake(self.vView1.bounds.size.width, self.vView1.bounds.size.height + 10.0f)];
    [self.vView3 setHidden:YES];
    
    [self.vView2 setRnUIViewTouchDelegate:self];
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    
    [svScrollView release];
    [vView1 release];
    [vView2 release];
    
    [super dealloc];
}

#pragma mark - implements UIScrollViewDelegate
/**
 * Scroll 시 계속 호출됨.
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"scrollViewDidScroll contentsOffset.x=%f, contentsOffset.y=%f", scrollView.contentOffset.x, scrollView.contentOffset.y);
//    [self.vView2 setCenter:CGPointMake(, self.topViewPoint.y - (scrollView.contentOffset.y))];
    if(showSubView == NO && scrollView.contentOffset.y < 0)
        [self.vView2 setFrame:CGRectMake(0.0f, self.topViewPoint.y - (scrollView.contentOffset.y), [self.vView2 frame].size.width, [self.vView2 frame].size.height)];
}

/**
 * Tells the delegate when the scroll view is about to start scrolling the content.
 * Parameters
 *   scrollView
 *     The scroll-view object that is about to scroll the content view.
 * Discussion
 *   The delegate might not receive this message until dragging has occurred over a small distance.
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewWillBeginDragging");
//    [self.vView3 setFrame:CGRectMake(0.0f, -(self.vView2.frame.size.height - scrollView.contentOffset.y), self.vView2.frame.size.width, self.vView2.frame.size.height)];
    
}

/**
 * Tells the delegate when the user finishes scrolling the content.
 * 
 * Parameters
 *   scrollView
 *     The scroll-view object where the user ended the touch..
 *   velocity
 *     The velocity of the scroll view (in points) at the moment the touch was released.
 *   targetContentOffset
 *     The expected offset when the scrolling action decelerates to a stop.
 * Discussion
 *   This method is not called when the value of the scroll view’s pagingEnabled property is YES. Your application can change the value of the targetContentOffset parameter to adjust where the scrollview finishes its scrolling animation.
 */
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    NSLog(@"scrollViewWillEndDragging velocity.x=%f, velocity.y=%f, TargetContentOffset x=%f, y=%f", scrollView.contentOffset.x, scrollView.contentOffset.y, targetContentOffset->x, targetContentOffset->y);
    if(scrollView.contentOffset.y < -50)
    {
        self.showSubView = YES;
        [self.svScrollView setScrollEnabled:NO];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.7f];
        
        [self.vView2 setFrame:CGRectMake(0.0f, -100.0f, [self.vView2 frame].size.width, [self.vView2 frame].size.height)];
        
        [UIView commitAnimations];
    }
}

/**
 * Tells the delegate when dragging ended in the scroll view.
 * 
 * Parameters
 *   scrollView
 *     The scroll-view object that finished scrolling the content view.
 *   decelerate
 *     YES if the scrolling movement will continue, but decelerate, after a touch-up gesture during a dragging operation. If the value is NO, scrolling stops immediately upon touch-up.
 * Discussion
 *   The scroll view sends this message when the user’s finger touches up after dragging content. The decelerating property of UIScrollView controls deceleration.
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"scrollViewDidEndDragging");
}

/**
 * Asks the delegate if the scroll view should scroll to the top of the content.
 * Parameters
 *   scrollView
 *     The scroll-view object requesting this information.
 *   Return Value
 *     YES to permit scrolling to the top of the content, NO to disallow it.
 * 
 * Discussion
 *   If the delegate doesn’t implement this method, YES is assumed. For the scroll-to-top gesture (a tap on the status bar) to be effective, the scrollsToTop property of the UIScrollView must be set to YES.
 */
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewShouldScrollToTop");
    if(self.showSubView == YES)
        return NO;
    return YES;
}

/**
 * Tells the delegate that the scroll view scrolled to the top of the content.
 *
 * Parameters
 *   scrollView
 *     The scroll-view object that perform the scrolling operation.
 *   Discussion
 *     The scroll view sends this message when it finishes scrolling to the top of the content. It might call it immediately if the top of the content is already shown. For the scroll-to-top gesture (a tap on the status bar) to be effective, the scrollsToTop property of the UIScrollView must be set to YES.
 */
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScrollToTop");
}

/**
 * Tells the delegate that the scroll view is starting to decelerate the scrolling movement.
 *
 * Parameters
 *   scrollView
 *     The scroll-view object that is decelerating the scrolling of the content view.
 *   Discussion
 *     The scroll view calls this method as the user’s finger touches up as it is moving during a scrolling operation; the scroll view will continue to move a short distance afterwards. The decelerating property of UIScrollView controls deceleration.
 */
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewWillBeginDecelerating");
}

/**
 * Tells the delegate that the scroll view has ended decelerating the scrolling movement.
 *
 * Parameters
 *   scrollView
 *     The scroll-view object that is decelerating the scrolling of the content view.
 * Discussion
 *   The scroll view calls this method when the scrolling movement comes to a halt. The decelerating property of UIScrollView controls deceleration.
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidEndDecelerating");
}

#pragma -mark RNUIViewDelegate Implements
-(void)singleTabTouch
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDidStopSelector:@selector(stopAnimationView: finished: context:)];
    [self.vView2 setFrame:CGRectMake(0.0f, self.topViewPoint.y, [self.vView2 bounds].size.width, [self.vView2 bounds].size.height)];    
    [UIView commitAnimations];
}

- (void) stopAnimationView:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
{
    
    [self.svScrollView setScrollEnabled:YES];
    self.showSubView = NO;
}
-(void)doubleTabTouch
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDidStopSelector:@selector(stopAnimationView: finished: context:)];
    [self.vView2 setFrame:CGRectMake(0.0f, -[self.vView2 bounds].size.height, [self.vView2 bounds].size.width, [self.vView2 bounds].size.height)];

    [UIView commitAnimations];
}

-(void)moveTouchPreviousLocation:(CGPoint)prePoint currentLocation:(CGPoint)curPoint
{
    float move = curPoint.y - prePoint.y;
    if((move + [self.vView2 frame].origin.y <= -100) && (move + [self.vView2 frame].origin.y >= self.topViewPoint.y))
        [self.vView2 setFrame:CGRectMake(0.0f, [self.vView2 frame].origin.y + move, [self.vView2 frame].size.width, [self.vView2 frame].size.height)];
}

-(void)endMoveTouch
{
    if(([self.vView2 frame].origin.y > (self.topViewPoint.y + 50)) && self.showSubView == NO)
    {
        // down animation
        self.showSubView = YES;
        [self.svScrollView setScrollEnabled:NO];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.7f];
        
        [self.vView2 setFrame:CGRectMake(0.0f, -100.0f, [self.vView2 frame].size.width, [self.vView2 frame].size.height)];
        
        [UIView commitAnimations];
    }
    else
    {
        // Top Animation
         [self.svScrollView setScrollEnabled:NO];
        [self singleTabTouch];
    }
}
@end
